<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测试结果" prop="testResult">
        <el-input
          v-model="queryParams.testResult"
          placeholder="请输入测试结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测试时长" prop="testTime">
        <el-input
          v-model="queryParams.testTime"
          placeholder="请输入测试时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电流" prop="current">
        <el-input
          v-model="queryParams.current"
          placeholder="请输入电流"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电压" prop="voltage">
        <el-input
          v-model="queryParams.voltage"
          placeholder="请输入电压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设定温度" prop="temperature">
        <el-input
          v-model="queryParams.temperature"
          placeholder="请输入设定温度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="震动时间" prop="shakeTime">
        <el-input
          v-model="queryParams.shakeTime"
          placeholder="请输入震动时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="震动频率" prop="shakeFrequency">
        <el-input
          v-model="queryParams.shakeFrequency"
          placeholder="请输入震动频率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="震动幅值" prop="shakeSize">
        <el-input
          v-model="queryParams.shakeSize"
          placeholder="请输入震动幅值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备id" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="数据状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入数据状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品id" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="波形数据" prop="waveformsId">
        <el-input
          v-model="queryParams.waveformsId"
          placeholder="请输入波形数据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['smart:SmartProductTest:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['smart:SmartProductTest:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['smart:SmartProductTest:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['smart:SmartProductTest:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SmartProductTestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="编码" align="center" prop="productCode" />
      <el-table-column label="测试结果" align="center" prop="testResult" />
      <el-table-column label="测试时长" align="center" prop="testTime" />
      <el-table-column label="电流" align="center" prop="current" />
      <el-table-column label="电压" align="center" prop="voltage" />
      <el-table-column label="设定温度" align="center" prop="temperature" />
      <el-table-column label="震动时间" align="center" prop="shakeTime" />
      <el-table-column label="震动频率" align="center" prop="shakeFrequency" />
      <el-table-column label="震动幅值" align="center" prop="shakeSize" />
      <el-table-column label="设备id" align="center" prop="deviceId" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据状态" align="center" prop="state" />
      <el-table-column label="产品id" align="center" prop="productId" />
      <el-table-column label="波形数据" align="center" prop="waveformsId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['smart:SmartProductTest:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['smart:SmartProductTest:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品测试对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="测试结果" prop="testResult">
          <el-input v-model="form.testResult" placeholder="请输入测试结果" />
        </el-form-item>
        <el-form-item label="测试时长" prop="testTime">
          <el-input v-model="form.testTime" placeholder="请输入测试时长" />
        </el-form-item>
        <el-form-item label="电流" prop="current">
          <el-input v-model="form.current" placeholder="请输入电流" />
        </el-form-item>
        <el-form-item label="电压" prop="voltage">
          <el-input v-model="form.voltage" placeholder="请输入电压" />
        </el-form-item>
        <el-form-item label="设定温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入设定温度" />
        </el-form-item>
        <el-form-item label="震动时间" prop="shakeTime">
          <el-input v-model="form.shakeTime" placeholder="请输入震动时间" />
        </el-form-item>
        <el-form-item label="震动频率" prop="shakeFrequency">
          <el-input v-model="form.shakeFrequency" placeholder="请输入震动频率" />
        </el-form-item>
        <el-form-item label="震动幅值" prop="shakeSize">
          <el-input v-model="form.shakeSize" placeholder="请输入震动幅值" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数据状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入数据状态" />
        </el-form-item>
        <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="波形数据" prop="waveformsId">
          <el-input v-model="form.waveformsId" placeholder="请输入波形数据" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-divider content-position="center">产品波形数据信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSmartProductTestWaveform">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSmartProductTestWaveform">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="smartProductTestWaveformList" :row-class-name="rowSmartProductTestWaveformIndex" @selection-change="handleSmartProductTestWaveformSelectionChange" ref="smartProductTestWaveform">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="数据" prop="data" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.data" placeholder="请输入数据" />
            </template>
          </el-table-column>
          <el-table-column label="注释" prop="notes" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.notes" placeholder="请输入注释" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.state" placeholder="请输入状态" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSmartProductTest, getSmartProductTest, delSmartProductTest, addSmartProductTest, updateSmartProductTest } from "@/api/smart/SmartProductTest";

export default {
  name: "SmartProductTest",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSmartProductTestWaveform: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品测试表格数据
      SmartProductTestList: [],
      // 产品波形数据表格数据
      smartProductTestWaveformList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,
        testResult: null,
        testTime: null,
        current: null,
        voltage: null,
        temperature: null,
        shakeTime: null,
        shakeFrequency: null,
        shakeSize: null,
        deviceId: null,
        createDate: null,
        state: null,
        productId: null,
        waveformsId: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品测试列表 */
    getList() {
      this.loading = true;
      listSmartProductTest(this.queryParams).then(response => {
        this.SmartProductTestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCode: null,
        testResult: null,
        testTime: null,
        current: null,
        voltage: null,
        temperature: null,
        shakeTime: null,
        shakeFrequency: null,
        shakeSize: null,
        deviceId: null,
        createDate: null,
        state: null,
        productId: null,
        waveformsId: null,
        userId: null
      };
      this.smartProductTestWaveformList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品测试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSmartProductTest(id).then(response => {
        this.form = response.data;
        this.smartProductTestWaveformList = response.data.smartProductTestWaveformList;
        this.open = true;
        this.title = "修改产品测试";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.smartProductTestWaveformList = this.smartProductTestWaveformList;
          if (this.form.id != null) {
            updateSmartProductTest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSmartProductTest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品测试编号为"' + ids + '"的数据项？').then(function() {
        return delSmartProductTest(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 产品波形数据序号 */
    rowSmartProductTestWaveformIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 产品波形数据添加按钮操作 */
    handleAddSmartProductTestWaveform() {
      let obj = {};
      obj.data = "";
      obj.notes = "";
      obj.state = "";
      obj.createDate = "";
      this.smartProductTestWaveformList.push(obj);
    },
    /** 产品波形数据删除按钮操作 */
    handleDeleteSmartProductTestWaveform() {
      if (this.checkedSmartProductTestWaveform.length == 0) {
        this.$modal.msgError("请先选择要删除的产品波形数据数据");
      } else {
        const smartProductTestWaveformList = this.smartProductTestWaveformList;
        const checkedSmartProductTestWaveform = this.checkedSmartProductTestWaveform;
        this.smartProductTestWaveformList = smartProductTestWaveformList.filter(function(item) {
          return checkedSmartProductTestWaveform.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSmartProductTestWaveformSelectionChange(selection) {
      this.checkedSmartProductTestWaveform = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('smart/SmartProductTest/export', {
        ...this.queryParams
      }, `SmartProductTest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
